package com.example.myapp;

import java.io.File;
import java.util.ArrayList;

import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
import android.widget.*;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class MainActivity extends Activity {
	
	public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
	public ImageView img;
	public Bitmap btm;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        //findViewById(R.id.main).setSystemUiVisibility(LinearLayout.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inScaled = false;
        o.inMutable = true;
    	btm = BitmapFactory.decodeFile((Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_PICTURES + "/colors.png") , o);
    	System.out.println(o.outWidth + " " + o.outHeight);
    	
    	img = (ImageView)findViewById(R.id.imageView1);
    	img.setImageBitmap(btm);
    	
    	System.out.println(img.getMaxWidth() + " " + img.getMaxHeight() + " - " + img.getMinimumWidth() + " " + img.getMinimumHeight());
    	btm.prepareToDraw();
    	
    	img.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				int xCor = 0;
				int yCor = 0;
				int action = 0;
				if((int)arg1.getX() < btm.getWidth() - 25 & (int)arg1.getX() >= 25 & (int)arg1.getY() < btm.getHeight() - 25 & (int)arg1.getY() >25) {
				xCor = (int)arg1.getX();
				yCor = (int)arg1.getY();
				action = arg1.getAction();
				} else {
					action = 3;					
				}
				
				switch (action) {
				
					case MotionEvent.ACTION_DOWN:
						System.out.println("Down" + xCor + " " + yCor);
						
						break;
					
					case MotionEvent.ACTION_MOVE:
						System.out.println("Move" + xCor + " " + yCor + "" + btm.getPixel(xCor, (int)(yCor*1.1)));
						for(int x = -25; x < 25; x++){
							for(int y = -25; y < 25; y++){
								btm.setPixel(xCor + x, (int)(yCor*1.1) + y, (btm.getPixel(xCor, (int)(yCor*1.1))) / 2);	
							}						
						}
						break;
					
					case MotionEvent.ACTION_UP:
						System.out.println("Up" + xCor + " " + yCor);
						
						break;
						
					case MotionEvent.ACTION_CANCEL:
						System.out.println("Cancel" + xCor + " " + yCor);
						
						break;
					
					}
				
				img.invalidate();
				return true;
			}
		});
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_open:
            	File mfile=new File(Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_PICTURES);
            	File[] list=mfile.listFiles();
            	for(int i=0;i<mfile.listFiles().length;i++)
            	{
            		if(list[i].isFile()){
            			System.out.println(list[i].getAbsolutePath());
            		}
            		
            	}
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    
    public void msbox(String str,String str2)
    {
        AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);                      
        //dlgAlert.setTitle(str); 
        TextView customTitleView = new TextView(getApplicationContext());
        customTitleView.setText(str);
        //customTitleView.setTextColor(Integer.parseInt(str));
        customTitleView.setTextSize(30);
		dlgAlert.setCustomTitle(customTitleView);
        dlgAlert.setMessage(str2); 
        dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                 //finish(); 
            }
       });
        dlgAlert.setCancelable(true);
        dlgAlert.create().show();
    }
    
}


